//
//  UIBarButtonItem+Extension.swift
//  WeiBo
//
//  Created by Apple on 16/9/4.
//  Copyright © 2016年 KY. All rights reserved.
//

import UIKit

extension UIBarButtonItem {
    //  便利构造函数创建UIBarButtonItem对象
    convenience init(title: String, imageName: String? = nil, target: Any?, action: Selector) {
        //  调用其他构造函数创建对象
        self.init()
        let button = UIButton()
        //  图片不为nil给其设置图片
        if imageName != nil {
            button.setImage(UIImage(named: imageName!), for: .normal)
        }
        //  添加点击事件
        button.addTarget(target, action: action, for: .touchUpInside)
        button.setTitle(title, for: .normal)
        button.setTitleColor(UIColor.darkGray, for: .normal)
        button.setTitleColor(UIColor.orange, for: .highlighted)
        button.titleLabel?.font = UIFont.systemFont(ofSize: 15)
        //  设置按钮大小
        button.sizeToFit()
        
        //  设置自定义视图
        customView = button
    }
}
